package gizmoball;

import gizmoball.Game.AnimationWindow;

import java.awt.Color;
import java.awt.Graphics;
//import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
//import java.awt.geom.Point2D;
import java.lang.Math;

public class Baffle {
	
	private int bwidth = 40;
	private int bheight = 10;
	private int b2width =40;
	private int b2height=10;
	private int b3width =40;
	private int b3height=10;
	private int bx ; 
	private int by = 493; // 閻牠鏁撻弬銈嗗闁跨喐鏋婚幏鐑芥晸鏉堝啳顫楃喊澶嬪闁跨喐鏋婚幏鐑芥晸閺傘倖瀚�
	private int b2x = bx+5;
	private int b2y = by;
	private int b3x = bx+35;
	private int b3y = by;
    int left_deg = 0 ;
    double left_rad = Math.toRadians(left_deg);
    int right_deg = 0 ;
    double right_rad = Math.toRadians(right_deg);
    
    private boolean judge_right=false;
    private boolean judge_left=false;
    
    public int x1;
    public int y1;
    public int x2;
    public int y2;
    public int x3;
    public int y3;
    
    public int x4;
    public int y4;
    public int x5;
    public int y5;
    public int x6;
    public int y6;



	private int target;
	
	public void setjudge_left(boolean mode){
	    judge_left = mode;
	}
	
	public void setjudge_right(boolean mode){
		judge_right = mode;
    }

	/**
	 * Constructor.
	 * 
	 * @param win
	 *            Animation window that will be drawing this ball.
	 */
	public Baffle(AnimationWindow win) {
		
	}

	private void leftmove() {
		bx -= 5;
		b2x -=5;
		b3x -=5;
	}

	private void rightmove() {
		bx += 5;
		b2x += 5;
		b3x += 5;
	}

	public void move(int target) {
		this.target = target;
		move();
	}

	public void move() {
		if (this.bx + this.bwidth / 2 > this.target) {
			this.leftmove();
			
			if (this.bx + this.bwidth / 2 < this.target) // 闁跨喐鏋婚幏閿嬵剾闁跨喐鏋婚幏鐑芥晸閺傘倖瀚圭拠娆撴晸閿燂拷
			{
				this.target = this.bx + this.bwidth / 2;
			}
		} 
		else if (this.bx + this.bwidth / 2 < this.target) {
			this.rightmove();
			if (this.bx + this.bwidth / 2 > this.target) // 闁跨喐鏋婚幏閿嬵剾闁跨喐鏋婚幏鐑芥晸閺傘倖瀚圭拠娆撴晸閿燂拷
			{
				this.target = this.bx + this.bwidth / 2;
			}
		}
	}
	
	
	
	
	public void leftturn(){
		if(this.judge_left==true)
		{
			if(left_deg<=40)
				left_deg+=4;
		}
		
		else if(this.judge_left==false)
    	{
    		if(left_deg>0)
    			left_deg-=4;
    	}
			 left_rad = Math.toRadians(left_deg);
			 x4=(int)(b2x-Math.cos(left_rad)*(b2width));
			 y4=(int)(b2y-(Math.sin(left_rad)*(b2width)));
			 x5=(int)(b2x-Math.sin(left_rad)*(b2height));
			 y5=(int)(b2y+(Math.cos(left_rad)*(b2height)));
			 x6=(int)(x5-Math.cos(left_rad)*(b2width));
			 y6=(int)(y5-(Math.sin(left_rad)*(b2width)));
	}
	public void rightturn(){
		if(this.judge_right==true)
		{
			if(right_deg<=40)
				right_deg+=4;
		}
		
		else if(this.judge_right==false)
    	{
    		if(right_deg>0)
    			right_deg-=4;
    	}
		
			 right_rad = Math.toRadians(right_deg);
			 x1=(int)(b3x+Math.cos(right_rad)*(b3width));
			 y1=(int)(b3y-(Math.sin(right_rad)*(b3width)));
			 x2=(int)(b3x+Math.sin(right_rad)*(b3height));
			 y2=(int)(b3y+Math.cos(right_rad)*(b3height));
			 x3=(int)(x2+Math.cos(right_rad)*b3width);
			 y3=(int)(y2-(Math.sin(right_rad)*(b3width)));
			 
		//	 System.out.println(status);
	    //if(status)
			//System.out.println("yyy");
		
		
	}
	public void paint(Graphics g) {
		// the "clip rectangle" is the area of the screen that needs to be
		// modified
		Color bc = new Color(0,0,0);
		g.setColor(bc);
		
		Rectangle clipRect = g.getClipBounds();
		Rectangle baffle = new Rectangle(this.bx, this.by, this.bwidth,
				this.bheight);
		
	    int rx[] = {this.b3x, this.x1, this.x3, this.x2};
       	int ry[] = {this.b3y, this.y1, this.y3, this.y2};
      
      
        int lx[] = {this.b2x, this.x5, this.x6, this.x4};
       	int ly[] = {this.b2y, this.y5, this.y6, this.y4};
       	Polygon polygon2 = new Polygon(lx,ly,4);
     	Polygon polygon1 = new Polygon(rx,ry,4);
        g.fillPolygon(polygon1);  
        g.fillPolygon(polygon2);  
        
		if (clipRect.intersects(baffle)) 
		{
			bc = new Color(108,123,139);
			g.setColor(bc);
			g.fillRect(bx, by, bwidth, bheight);
			
		}
	}

	public int collision(int x, int y, int radius) {
		if (y + radius <= this.by)
			return -1;
		else if ((x >= b2x-b2width) 
				&& x<=b2x 
				&& (y + radius > b2y))
		{ 
			   	if(left_deg != 0)
			       return 45;
			  	else
				   return 0;
		}
		
		else if ((x<=b3x+b3width) 
				&& x>=b3x 
				&& (y+radius) > b3y - (x-b3x))
		{
			 	if(right_deg != 0)
			       return 315;
			 	else
				   return 0;
		}
		else if (x + radius > this.bx && x - radius < this.bx + bwidth
				&& y + radius > this.by && y < this.by) {
			y = this.by - radius;
			return 0;
		} 
		else if (x - radius < this.bx + this.bwidth + this.b3width && x > this.bx + this.bwidth/2
				&& y + radius > this.by
				&& y - radius < (this.by + this.bheight)) {
			x = this.bx + this.bwidth + radius + this.b3width;
			if(right_deg == 0)
				return 90;
			else
				return -1;
		} 
		else if (x + radius > this.bx - this.b2width && x < this.bx + this.bwidth/2
				&& y + radius > this.by
				&& y - radius < (this.by + this.bheight)) {
			x = this.bx - radius - this.b2width;
			if(left_deg == 0)
				return 270;
			else
				return -1;
		}

		else
			return -1;
	}
}